﻿@{
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery-2.0.3.js"></script>
        <script src="~/LiberUI/Scripts/jquery.ba-resize.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Controllers.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
        <script src="~/LiberUI/Scripts/jquery.textSearch-1.0.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.UI.js"></script>
        <script src="~/SiteUI/BaseScripts/site.js"></script>
    }
    <link href="~/SiteUI/Task/Task.css" rel="stylesheet" />
}
<!--任务详情页面-->
<div id="viewTaskInfo" style="display:none">
    <div class="SectionTitle" id="Head">
        <h3>请选择一个任务来查看</h3>
        <div id="TaskActions">
            <div id="btnComplete">提交完成</div>
            <div id="btnCancel">取消</div>
        </div>
    </div>
    <div id="ColLeft">
        <!--如果任务是由其他业务数据衍生出来的，需要显示相关业务的基本信息-->
        @*<pre id="TaskFrom"></pre>*@

        <!--如果任务依赖于前一个任务的完成，可拒绝或接受，拒绝则当前任务会挂起-->
        @*<div id="PreTask">前置任务，可接受或拒绝</div>*@

        <h5 id="TaskDeadline"></h5>
        <pre id="TaskContent"></pre>
        <!--任务相关业务数据：指为完成任务而所做的，需记录到系统中的工作，如上传的文档。由任务类型决定-->
        <div id="TaskData"></div>
        <div class="SectionTitle">
            评论
        </div>
        
        <div id="btnNewComment">发表你的看法</div>
        <div id="pnlComment" style="clear:both"></div>
    </div>
    <div id="ColRight">
        @*<div class="BlockTitle" id="TaskStatus">状态</div>
        <br />*@
        <div id="TaskImportance"></div>
        <br />
        <label>任务类型：</label>
        <h4 class="TaskBaseInfo" id="TaskType"></h4>
        <br />
        <label>关联子系统：</label>
        <h4 class="TaskBaseInfo" id="TaskSubSystem"></h4>
        <br />
        <label>可结算工时：</label>
        <h4 class="TaskBaseInfo" id="TaskTimeValue"></h4>
        <br />
        <label>实际耗时：</label>
        <h4 class="TaskBaseInfo" id="TaskTimeCost"></h4>
    </div>
</div>
<div style="display:none">
    @RenderPage("/SiteUI/Comment/wgComment.cshtml")
</div>
<script>
    wgTask = {}
    wgTask.CTask = null;
    wgTask.Model = new TaskModel();
    wgTask.lastLogId = -1;
    wgTask.vf_OnTaskUpdated;

    //创建一个标准任务卡片
    wgTask.MakeTaskCard = function (task) {
        var card = $("<div>").addClass('TaskCard');
        card.addClass(task.Status);
        if (task.Status == 'Completed') {
            card.append($("<h4>").text('已完成'));
        }
        else {
            if (task.Delay > 0) {
                card.append($("<h4>").text('已超期：' + task.DelayInfo));
                card.addClass("deferred");
            }
            else if (task.Delay > -300) {
                card.append($("<h4>").text('还有' + (0 - task.Delay) + '天到期'));
            }
            else {
                card.append($("<h4>").text('-'));
            }
        }
        card.append(new Rating(null, { ShowDescribe: false }).addClass('crdImportance').val(task.Importance).disable().dom());
        card.append($("<h5>").addClass('crdContent').text(task.Content));
        card.append($("<span>").addClass('crdProposeInfo').text(task.Proposer + ' ' + task.ProposeTime.split(' ')[0]));
        return card;
    };

    //加载一个任务及相关信息到任务详情页面
    wgTask.LoadTask = function (task, reloading) {
        if (wgTask.errTimeCost) {
            wgTask.errTimeCost.dispose();
            wgTask.errTimeCost = null;
        }
        wgTask.CTask = task;
        wgTask.Model.Bind(task);
        if (!reloading) {
            wgTask.vTaskView.hide().fadeIn();
        }

        $("#Head>h3").text('状态：'+wgTask.Model.GetFriendlyValue('Status')+' | '+task.Proposer + '于' + task.ProposeTime.split(' ')[0] + '提交此任务');

        //TODO:如果有来源信息

        //TODO:如果有前置任务

        //TODO:加载任务评论
        wgComment.lstComments.Clear();
        wgComment.chkComment.Reset();
        new AjaxHelper('get', '/Comment/Query', { objId: wgTask.CTask.IDStr }, getCommentEnd, wgTask.CTask.IDStr);

        //根据任务当前状态和用户权限设置任务可用的操作
        if (task.Status !== 'Active') {
            wgTask.vTaskView.btnComplete.disable();
        }
        else {
            wgTask.vTaskView.btnComplete.enable();
        }

        if (task.Status == 'Active' || task.Status == 'Draft')
        {
            wgTask.vTaskView.btnCancel.enable();
        }
        else {
            wgTask.vTaskView.btnCancel.disable();
        }

        if (task.Delay > 0) {
            $("#TaskDeadline").text('完成时限：' + task.Deadline.split(' ')[0]).addClass('deferred').removeClass('editble');
        }
        else if (task.Delay > -300) {
            $("#TaskDeadline").text('完成时限：' + task.Deadline.split(' ')[0]).removeClass('deferred editble');
        }
        else {
            $("#TaskDeadline").text('完成时限：-').removeClass('deferred');
        }

        $("#TaskContent").text(task.Content);

        wgTask.vTaskView.ratImportance.val(task.Importance).disable();
        $("#TaskType").text(task.Type);
        $("#TaskSubSystem").text(task.SubSystem);

        //判断有没有设置可结算人天
        if (task.TimeValue > 0) {
            $("#TaskTimeValue").text(task.TimeValue + '小时');
        }
        else {
            $("#TaskTimeValue").text('-');
        }
        //判断有没有填写实际耗时
        if (task.TimeCost > 0) {
            $("#TaskTimeCost").text(task.TimeCost + '小时');
        }
        else {
            $("#TaskTimeCost").text('-');
        }

        if (task.Status !== 'Completed') {
            $("#TaskTimeValue").addClass('editble').on('click', EditTimeValue);
            $("#TaskTimeCost").addClass('editble').on('click', EditTimeCost);
        }
        else {
            $("#TaskTimeValue").removeClass('editble').off('click');
            $("#TaskTimeCost").removeClass('editble').off('click');
        }
    };

    function EditTimeValue() {
        new FieldEditor().Editor($("<input type='number'>")).Active($(this), function (v) {
                                                                                            if (!ObjHelper.IsEmpty(v)&&v>=0)
                                                                                            {
                                                                                                new AjaxHelper('get', '/Task/SetTimeValue/', { id: wgTask.CTask.IDStr, timeValue: v }, editEnd);
                                                                                            }
                                                                                          });
    };

    function EditTimeCost() {
        if (wgTask.errTimeCost) {
            wgTask.errTimeCost.dispose();
            wgTask.errTimeCost = null;
        }
        new FieldEditor().Editor($("<input type='number'>")).Active($(this), function (v) {
                                                                                            if (!ObjHelper.IsEmpty(v)&&v>=0)
                                                                                            {
                                                                                                new AjaxHelper('get', '/Task/SetTimeCost', { id: wgTask.CTask.IDStr, timeCost: v }, editEnd);
                                                                                            }
                                                                                          });
    };

    function editEnd(rtn) {
        $('body').Unmask();
        if (rtn.IsPositive && rtn.Data.IDStr == wgTask.CTask.IDStr) {
            wgTask.CTask = rtn.Data;

            if (ObjHelper.IsFunc(wgTask.vf_OnTaskUpdated)) {
                wgTask.vf_onTaskUpdated(wgTask.CTask);
            }
            wgTask.LoadTask(wgTask.CTask,true);
        }
    };

    function CompleteTask() {
        if (wgTask.CTask.TimeCost <= 0) {
            if (wgTask.errTimeCost) {
                wgTask.errTimeCost.show();
            }
            else {
                wgTask.errTimeCost = new Bubble().ExStyle('Error').Attach($("#TaskTimeCost").prev('label').eq(0)).Content('请填写任务的实际耗时').show();
            }
            return;
        }
        else {
            if (wgTask.errTimeCost) {
                wgTask.errTimeCost.dispose();
                wgTask.errTimeCost = null;
            }
            $("body").Mask().addClass('saving');
            new AjaxHelper('get', '/Task/Complete/', { id: wgTask.CTask.IDStr }, editEnd);
        }
    };

    function CancelTask() {

    };

    function SubmitComment() {
        if (wgComment.chkComment.Check()) {
            var cmnt = wgComment.txbNewComment.val();
            wgTask.vTaskView.btnNewComment.disable('提交...');
            new AjaxHelper("post", "/Comment/New", { cnt: cmnt, objType: wgTask.Model.GetTypeName(), objId: wgTask.CTask.IDStr },getCommentEnd ,wgTask.CTask.IDStr);
        }
    };

    function getCommentEnd(rtn, state) {
        wgTask.vTaskView.btnNewComment.enable();
        if (rtn.IsPositive && state == wgTask.CTask.IDStr) {
            wgComment.txbNewComment.val('');
            wgComment.lstComments.Prepend(rtn.Data.Comments ? rtn.Data.Comments : rtn.Data);
            if (ObjHelper.IsFunc(wgTask.vf_onNewCommentOk)) {
                wgTask.vf_onNewCommentOk(rtn);
            }
        }
    };

    function onCommentEditing() {
        KeybordHelper.globalOn('ctrl+enter', SubmitComment);
    };
    function onCommentEditEnd() {
        KeybordHelper.globalOff('ctrl+enter', SubmitComment);
    };


    $(function () {
        //定义任务详细视图
        wgTask.vTaskView = $("#viewTaskInfo");
        wgTask.vTaskView.ratImportance = new Rating($("#TaskImportance"));
        
        wgTask.vTaskView.btnNewComment = new Button($("#btnNewComment")).on('click', SubmitComment);
        wgComment.txbNewComment.dom().insertBefore(wgTask.vTaskView.btnNewComment.dom());
        wgComment.txbNewComment.on('focus', onCommentEditing).on('blur', onCommentEditEnd);
        wgComment.lstComments.dom($("#pnlComment"));

        wgTask.vTaskView.btnComplete = new Button($("#btnComplete")).on('click', CompleteTask);
        wgTask.vTaskView.btnCancel = new Button($("#btnCancel"), {ExStyle:'risky'}).on('click', CancelTask);

        //定义mini任务列表
        wgTask.lstMiniTasks = new List(null, { ShowHead: false, ShowFoot: false });

        //定义任务类型选择菜单
        wgTask.mnuTaskType = new PopMenu();
        wgTask.lstTypes = new List(null, { ShowHead: false, ShowFoot: false, Selectble: true });
        wgTask.lstTypes.af_makeView = function (_data) {
            return $("<span>").addClass('taskTypeItem').text(_data.Value);
        };
        wgTask.mnuTaskType.Menu(wgTask.lstTypes, 'itemSlcted');

        //定义任务关联子系统菜单
        wgTask.mnuSubSystem = new PopMenu();
        wgTask.lstSubSystems = new List(null, { ShowHead: false, ShowFoot: false, Selectble: true });
        wgTask.lstSubSystems.af_makeView = function (_data) {
            return $("<span>").addClass('taskSubSystemItem').text(_data.Value);
        };
        wgTask.mnuSubSystem.Menu(wgTask.lstSubSystems, 'itemSlcted');
    })
</script>
